package com.es.disped.web.controller.settings;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.common.constant.RecordStatus;
import com.es.disped.common.model.MsgModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.MenuType;
import com.es.disped.dao.model.PermissionType;
import com.es.disped.dao.model.RoleType;
import com.es.disped.dao.model.UserType;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value="属性配置")
@Controller
@RequestMapping("/common/setting")
public class SettingController extends BaseController {

	private final String MENU="menu";
	private final String RECORD="record";
	private final String PERMISSION="permission";
	private final String ROLE="role";
	private final String USER="user";
	
	@ApiOperation(value="菜单类型",response=MsgModel.class,httpMethod="POST",notes="获取菜单类型")
	@RequestMapping(value="/property",method=RequestMethod.POST)
	public @ResponseBody MsgModel getProperties(@RequestBody @ApiParam(name="properties",value="需要获取的属性名称['menu','record','permission','role','user']",required=true) List<String> properties)
	{
		List<Map<String, String>> lists=new ArrayList<Map<String,String>>();
		for(String property:properties)
		{
			if(MENU.equals(property))
			{
				lists.add(getMenuTypes());
			}else if(RECORD.equals(property))
			{
				lists.add(getRecordStatusTypes());
			}else if(PERMISSION.equals(property))
			{
				lists.add(getPermissionTypes());
			}else if(ROLE.equals(property))
			{
				lists.add(getRoleTypes());
			}else if(USER.equals(property))
			{
				lists.add(getUserTypes());
			}
		}
		return new MsgModel(lists.size()>0?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				lists.size()>0?"":"获取失败", lists.size()>0?lists:"");
	}
	
	private Map<String, String> getUserTypes() {
		Map<String, String> map=new HashMap<String, String>();
		List<UserType> userTypes=Arrays.asList(UserType.values());
		for(UserType userType:userTypes)
		{
			map.put(userType.getValue(), userType.getName());
		}
		return map;
	}

	private Map<String, String> getRoleTypes() {
		Map<String, String> map=new HashMap<String, String>();
		List<RoleType> roleTypes=Arrays.asList(RoleType.values());
		for(RoleType roleType:roleTypes)
		{
			map.put(roleType.getValue(), roleType.getName());
		}
		return map;
	}

	private Map<String, String> getPermissionTypes() {
		Map<String, String> map=new HashMap<String, String>();
		List<PermissionType> permissionTypes=Arrays.asList(PermissionType.values());
		for(PermissionType permissionType:permissionTypes)
		{
			map.put(permissionType.getValue(), permissionType.getName());
		}
		return map;
	}

	private Map<String, String> getRecordStatusTypes() {
		Map<String, String> map=new HashMap<String, String>();
		List<RecordStatus> recordStatus=Arrays.asList(RecordStatus.values());
		for(RecordStatus status:recordStatus)
		{
			map.put(String.valueOf(status.getValue()), status.getName());
		}
		return map;
	}

	private Map<String, String> getMenuTypes() {
		Map<String, String> map=new HashMap<String, String>();
		List<MenuType> menuTypes=Arrays.asList(MenuType.values());
		for(MenuType menuType:menuTypes)
		{
			map.put(menuType.getValue(), menuType.getName());
		}
		return map;
	}
}
